import java.util.*;

public class First {
    private static final int RED_BALL_COUNT = 6;
    private static final int BLUE_BALL_COUNT = 1;
    private static final int RED_BALL_MIN = 1;
    private static final int RED_BALL_MAX = 33;
    private static final int BLUE_BALL_MIN = 1;
    private static final int BLUE_BALL_MAX = 16;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        boolean running = true;

        System.out.println("欢迎使用双色球开奖验证程序！");

        while (running) {
            showMenu();
            int choice = getIntInput(scanner, "请选择操作: ");

            switch (choice) {
                case 1:
                    generateMultipleTickets(scanner);
                    break;
                case 2:
                    redeemTicket(scanner);
                    break;
                case 3:
                    System.out.println("感谢使用，再见！");
                    running = false;
                    break;
                default:
                    System.out.println("无效选择，请重新输入！");
            }
        }
        scanner.close();
    }

    /**
     * 显示主菜单
     */
    private static void showMenu() {
        System.out.println("\n========== 双色球彩票系统 ==========");
        System.out.println("1. 生成5注彩票号码");
        System.out.println("2. 兑奖");
        System.out.println("3. 退出");
        System.out.println("==================================");
    }

    /**
     * 生成多注彩票号码
     */
    private static void generateMultipleTickets(Scanner scanner) {
        System.out.println("\n生成5注彩票号码:");
        for (int i = 0; i < 5; i++) {
            int[] redBalls = generateRedBalls();
            int blueBall = generateBlueBall();
            System.out.println("第" + (i + 1) + "注: " + formatBalls(redBalls, blueBall));
        }
        System.out.println("按回车键返回主菜单...");
        scanner.nextLine();
    }

    /**
     * 兑奖功能
     */
    private static void redeemTicket(Scanner scanner) {
        // 清除缓冲区中的换行符（如果存在）
        if (scanner.hasNextLine()) {
            scanner.nextLine();
        }

        System.out.println("\n兑奖功能");
        System.out.println("请输入您的投注号码（格式：03 12 18 22 27 33 09，前6个为红球，最后1个为蓝球）：");

        String input = scanner.nextLine().trim();
        String[] numbers = input.split("\\s+");

        if (numbers.length != 7) {
            System.out.println("输入格式错误！请按照示例格式输入7个数字。");
            return;
        }

        try {
            int[] userRedBalls = new int[6];
            for (int i = 0; i < 6; i++) {
                userRedBalls[i] = Integer.parseInt(numbers[i]);
                if (userRedBalls[i] < 1 || userRedBalls[i] > 33) {
                    System.out.println("红球号码应在1-33之间！");
                    return;
                }
                // 检查是否有重复号码
                for (int j = 0; j < i; j++) {
                    if (userRedBalls[i] == userRedBalls[j]) {
                        System.out.println("红球号码不能重复！");
                        return;
                    }
                }
            }

            int userBlueBall = Integer.parseInt(numbers[6]);
            if (userBlueBall < 1 || userBlueBall > 16) {
                System.out.println("蓝球号码应在1-16之间！");
                return;
            }

            // 随机生成开奖号码
            int[] drawRedBalls = generateRedBalls();
            int drawBlueBall = generateBlueBall();

            System.out.println("您的投注号码: " + formatBalls(userRedBalls, userBlueBall));
            System.out.println("开奖号码: " + formatBalls(drawRedBalls, drawBlueBall));

            // 判断中奖等级
            int prizeLevel = checkPrizeLevel(userRedBalls, userBlueBall, drawRedBalls, drawBlueBall);

            if (prizeLevel == 0) {
                System.out.println("很遗憾，您未中奖。");
            } else {
                String[] prizeNames = {"", "一等奖", "二等奖", "三等奖", "四等奖", "五等奖", "六等奖"};
                System.out.println("恭喜您中了" + prizeNames[prizeLevel] + "！");
            }

        } catch (NumberFormatException e) {
            System.out.println("输入格式错误！请输入数字。");
        }

        System.out.println("按回车键返回主菜单...");
        scanner.nextLine();
    }

    /**
     * 获取整数输入
     */
    private static int getIntInput(Scanner scanner, String prompt) {
        System.out.print(prompt);
        while (!scanner.hasNextInt()) {
            System.out.print("请输入有效数字: ");
            scanner.next();
        }
        return scanner.nextInt();
    }

    /**
     * 判断中奖等级
     *
     * @param userRedBalls 用户红球号码
     * @param userBlueBall 用户蓝球号码
     * @param drawRedBalls 开奖红球号码
     * @param drawBlueBall 开奖蓝球号码
     * @return 中奖等级(1-6), 0表示未中奖
     */
    public static int checkPrizeLevel(int[] userRedBalls, int userBlueBall, int[] drawRedBalls, int drawBlueBall) {
        // 计算红球匹配数量
        int redMatchCount = 0;
        for (int userRed : userRedBalls) {
            for (int drawRed : drawRedBalls) {
                if (userRed == drawRed) {
                    redMatchCount++;
                    break;
                }
            }
        }

        // 判断蓝球是否匹配
        boolean blueMatch = (userBlueBall == drawBlueBall);

        // 根据规则判断中奖等级
        if (redMatchCount == 6 && blueMatch) {
            return 1; // 一等奖
        } else if (redMatchCount == 6 && !blueMatch) {
            return 2; // 二等奖
        } else if (redMatchCount == 5 && blueMatch) {
            return 3; // 三等奖
        } else if ((redMatchCount == 5 && !blueMatch) || (redMatchCount == 4 && blueMatch)) {
            return 4; // 四等奖
        } else if ((redMatchCount == 4 && !blueMatch) || (redMatchCount == 3 && blueMatch)) {
            return 5; // 五等奖
        } else if (redMatchCount <= 2 && blueMatch) {
            return 6; // 六等奖
        } else {
            return 0; // 未中奖
        }
    }

    /**
     * 生成6个不重复的红球号码(1-33)
     *
     * @return 红球号码数组
     */
    private static int[] generateRedBalls() {
        Random random = new Random();
        Set<Integer> redBalls = new HashSet<>();

        while (redBalls.size() < 6) {
            int ball = random.nextInt(33) + 1;
            redBalls.add(ball);
        }

        return redBalls.stream().mapToInt(Integer::intValue).toArray();
    }

    /**
     * 生成1个蓝球号码(1-16)
     *
     * @return 蓝球号码
     */
    private static int generateBlueBall() {
        Random random = new Random();
        return random.nextInt(16) + 1;
    }

    /**
     * 格式化显示号码
     *
     * @param redBalls 红球数组
     * @param blueBall 蓝球号码
     * @return 格式化后的字符串
     */
    private static String formatBalls(int[] redBalls, int blueBall) {
        StringBuilder sb = new StringBuilder();
        Arrays.sort(redBalls); // 排序以便显示
        for (int i = 0; i < redBalls.length; i++) {
            if (i > 0) sb.append(" ");
            if (redBalls[i] < 10) sb.append("0");
            sb.append(redBalls[i]);
        }
        sb.append(" | ");
        if (blueBall < 10) sb.append("0");
        sb.append(blueBall);
        return sb.toString();
    }
}